// 引入包 =======================================================================
import { createApp } from 'vue'
import 'nprogress/nprogress.css'
import '@/utils/dayjs-utils'
// 引入本地文件 ==================================================================
import '@/assets/css/index.scss'
import '@/assets/icons/index'
import { Pinia } from '@/store'
import { getTokenByTicket } from '@/assets/api/user'
import useApp from '@/store/app'
import { useDebounceDirective, useThrottleDirective } from '@/directives/directives'
import directives from '@/directives'
import { Router } from '@/router'
// 引入本地组件 ==================================================================
import App from './App.vue'
import Emoji from '@/components/emoji/Vue3Emoji.umd'
import MentionUser from '@/components/post-text/components/MentionUser.vue'
// 加载应用 =====================================================================
const appUse = (app) => {
  app.use(Router)
  app.use(Pinia)
  app.use(directives)
  // 使用防抖
  app.directive('debounce', useDebounceDirective(300))
  // 使用节流
  app.directive('throttle', useThrottleDirective(300))
  app.component('MentionUser', MentionUser)
  app.component('Emoji', Emoji)
}

// render ======================================================================
let app
const render = async () => {
  let token = null
  let ticket = window.location.search.match(/ticket=(.+)/)
  if (ticket) {
    const res = await getTokenByTicket({ ticket: ticket[1] })
    if (res.code === 200) {
      token = res.data
    }
  }
  app = createApp(App)
  appUse(app)
  const appStore = useApp()
  if (token) {
    appStore.setToken(token)
  }
  app.mount('#app')
}

render()
